package com.link.base.base.fund.service;

import com.link.base.base.fund.model.DealerAccount;
import com.link.base.base.fund.model.DealerTransactionDetail;
import com.link.base.base.order.model.SaleOrder;
import com.link.base.base.order.service.SaleOrderService;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.DateUtil;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/12 10:10
 * @version 1.0
 */

@Service
public class CommonTransactionServiceImpl implements CommonTransactionService {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private DealerAccountService dealerAccountService;

    @Resource
    private DealerTransactionDetailService dealerTransactionDetailService;

    @Resource
    private SaleOrderService saleOrderService;

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void changeAccount(Long dealerAccountId, Long transactionDetailId, double transactionAmount, Long sourceId, String sourceNum,
                              String sourceType, String textDigest, String comments) throws Exception {
        if (null == dealerAccountId) {
            throw new ServiceException("FUND-011");
        }
        DealerAccount dealerAccount = new DealerAccount();
        dealerAccount.setId(dealerAccountId);
        dealerAccount = dealerAccountService.queryById(dealerAccount);
        if (dealerAccount == null) {
            throw new ServiceException("FUND-012");
        }
        double accountBeforeAmount = dealerAccount.getAmount();
        double accountAfterAmount = accountBeforeAmount + transactionAmount;
        if (accountAfterAmount < 0) {
            throw new ServiceException("FUND-013");
        }
        DealerTransactionDetail transaction = new DealerTransactionDetail();
        if (null == transactionDetailId) {
            transaction.setId(keyGenerateService.keyGenerate());
        } else {
            transaction.setId(transactionDetailId);
        }

        transaction.setHeadId(dealerAccount.getId());
        //来源-充值
        transaction.setSourceType(sourceType);
        //来源Id
        transaction.setSourceNum(sourceNum);
        //来源编码
        transaction.setSourceId(sourceId);
        //摘要
        transaction.setTextDigest(textDigest);
        //备注
        transaction.setComments(comments);
        //交易时间
        transaction.setDealTime(DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
        //交易金额
        transaction.setAmount(transactionAmount);
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
        //期初余额
        transaction.setRemainAmount(df.format(dealerAccount.getAmount()));
        //期末余额
        transaction.setTotalAmount(df.format(accountAfterAmount));
        transaction.setRow_status("NEW");
        dealerTransactionDetailService.insert(transaction);
        dealerAccount.setAmount(accountAfterAmount);
        dealerAccount.setRow_status("UPDATE");
        dealerAccountService.update(dealerAccount);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void orderPay(Long orderId, Long dealerAccountId, String textDigest, String comments) throws Exception {
        SaleOrder order = new SaleOrder();
        order.setId(orderId);
        order = saleOrderService.queryById(order);
        String flag = "Y";
        if (order == null) {
            throw new ServiceException("FUND-014");
        } else if (flag.equals(order.getIsPaid())) {
            throw new ServiceException("FUND-015");
        } else {
            if (StringUtils.isBlank(textDigest)) {
                textDigest = "订单【" + order.getOrderNo() + "】付款";
            }
            changeAccount(dealerAccountId, null,
                    0 - order.getOrderTotalAmount(), order.getId(), order.getOrderNo(), "Order Deduct", textDigest, comments);
            order.setIsPaid("Y");
            saleOrderService.paidUpdate(order);
        }
    }

}
